package com.ocean.array;

/**
 * https://leetcode.cn/problems/ti-huan-kong-ge-lcof/
 * <p>
 * 请实现一个函数，把字符串 s 中的每个空格替换成"%20"。
 * <p>
 *  
 * <p>
 * 示例 1：
 * <p>
 * 输入：s = "We are happy."
 * 输出："We%20are%20happy."
 *  
 * <p>
 * 限制：
 * <p>
 * 0 <= s 的长度 <= 10000
 *
 * @author linmiaolai@sanyygp.com<br>
 * @version 1.0<br>
 * @date 2023/03/28 <br>
 */
public class TiHuanKongGeLcof {
    public static void main(String[] args) {
        TiHuanKongGeLcof kongGeLcof = new TiHuanKongGeLcof();
        System.out.println(kongGeLcof.replaceSpace("We are happy."));
    }

    public String replaceSpace(String s) {
        if (s == null || s.length() == 0) {
            return s;
        }
        int n = s.length();
        char[] chars = new char[n * 3];
        int index = 0;
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == ' ') {
                chars[index++] = '%';
                chars[index++] = '2';
                chars[index++] = '0';
            } else {
                chars[index++] = s.charAt(i);
            }
        }
        return new String(chars, 0, index);
    }
}
